package org.c797797ty.railway;
import org.c797797ty.railway.model.User;
import org.c797797ty.railway.service.MessageService;
import org.c797797ty.railway.service.TicketService;
import org.c797797ty.railway.service.UserService;
import org.c797797ty.railway.util.DBUtil;

import java.util.Scanner;

/**
 * @author C797797ty
 * @data 2025/11/3 14:17
 */


public class RailwayTicketSystem {
    private static Scanner scanner = new Scanner(System.in);
    private static User currentUser = null;

    public static void main(String[] args) {
        while (true) {
            if (currentUser == null) {
                showMainMenu();
            } else {
                showUserMenu();
            }
        }
    }

    private static void showMainMenu() {
        System.out.println("\n===== 铁路购票系统 =====");
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 退出");
        System.out.print("请选择: ");

        int choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        switch (choice) {
            case 1:
                currentUser = UserService.login();
                break;
            case 2:
                UserService.register();
                break;
            case 3:
                System.out.println("感谢使用，再见！");
                DBUtil.closeConnection();
                System.exit(0);
                break;
            default:
                System.out.println("无效选择！");
        }
    }

    private static void showUserMenu() {
        // 显示未读消息数量
        int unreadCount = MessageService.getUnreadMessageCount(currentUser.getId());
        String messageIndicator = unreadCount > 0 ? " [" + unreadCount + "条未读消息]" : "";

        System.out.println("\n===== 用户菜单" + messageIndicator + " =====");
        System.out.println("欢迎，" + currentUser.getUsername() + "！");
        System.out.println("1. 查看所有车票");
        System.out.println("2. 查询车票");
        System.out.println("3. 购票");
        System.out.println("4. 帮他人购票");
        System.out.println("5. 查看我的订单");
        System.out.println("6. 退票");
        System.out.println("7. 查看系统消息");
        System.out.println("8. 退出登录");
        System.out.print("请选择: ");

        int choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        switch (choice) {
            case 1:
                TicketService.displayAllTickets();
                break;
            case 2:
                TicketService.searchTickets();
                break;
            case 3:
                TicketService.bookTicket(currentUser);
                break;
            case 4:
                TicketService.bookTicketForOther(currentUser);
                break;
            case 5:
                TicketService.viewMyTickets(currentUser);
                break;
            case 6:
                TicketService.cancelTicket(currentUser);
                break;
            case 7:
                MessageService.viewSystemMessages(currentUser);
                break;
            case 8:
                currentUser = null;
                System.out.println("已退出登录");
                break;
            default:
                System.out.println("无效选择！");
        }
    }
}